﻿using OVS_FormsRecognition.Manager;
using System.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using OVS.BLL;
using OVS.BusinessObjects;
namespace OVS_SizeCalculation.Manager
{
    public class SizeCalculationManager
    {


        #region attributs
       // Label _LabelLastAlertInfo;
        private double sizeH;
        private double sizeL;
        public Image<Bgr, Byte> myImage;
        public Rectangle myRect;
        private List<Image_Details> listID;
        private int imdeID;
        #endregion

        /// <summary>
        /// Logger par défaut du ManagerUtilisateurs
        /// </summary>
        private static readonly log4net.ILog _Log = log4net.LogManager.GetLogger(typeof(SizeCalculationManager));

        #region Gestion du singleton

        /// <summary>
        /// Instance locale pour héberger le singleton
        /// </summary>
        static SizeCalculationManager instance = null;

        /// <summary>
        /// Objet permettant la gestion des LOCK lors de l'instanciation
        /// </summary>
        static readonly object padlock = new object();

        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        SizeCalculationManager()
        {
            if (_Log.IsDebugEnabled)
                _Log.DebugFormat("Nouvelle instance de '{0}'", this.GetType().ToString());
            
        }

        
        /// <summary>
        /// Déclaration unique par gestion du singleton
        /// </summary>
        public static SizeCalculationManager Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new SizeCalculationManager();
                    }
                    return instance;
                }
            }
        }

        #endregion Gestion du singleton

      

        /// <summary>
        /// Registers the event handler.
        /// </summary>
        /// <param name="pSizeCalculationStartEvent">The p size calculation start event.</param>
        public void registerEventHandler()
        {
            //FormsRecognitionManager.Instance.SizeCalculationStartEvent += new FormsRecognitionManager.SizeCalculationEventHandler(CallBackSizeCalculationStartEvent);
        }

        /// <summary>
        /// Calls the back size calculation start event.
        /// </summary>
        /// <param name="nullObject">The null object.</param>
        /// <param name="pSizeCalculationEventArgs">The <see cref="SizeCalculationEventArgs" /> instance containing the event data.</param>
        private void CallBackSizeCalculationStartEvent(object nullObject, SizeCalculationEventArgs pSizeCalculationEventArgs)
        {
            _Log.Debug("new event ListenerSizeCalculationStartEvent !!!");

            //if (_LabelLastAlertInfo != null)
            //{
            //    _LabelLastAlertInfo.Content = DateTime.Now.ToString();

            //}
            listID = new List<Image_Details>();
            sizeH = 0;
            sizeL = 0;
            sizeH = getSizeH(pSizeCalculationEventArgs.sceImage, pSizeCalculationEventArgs.sceRect);
            sizeL = getSizeL(pSizeCalculationEventArgs.sceImage, pSizeCalculationEventArgs.sceRect);
            //Insertion en base des informations
            //On connait l'id de l'image de l'alerte img_id et l'id de l'image haar img_haar_id
            Image_Details objImg = new Image_Details();
                       
            
            objImg.imha_id=pSizeCalculationEventArgs.img_haar_id;
            objImg.imde_sizeH = sizeH;
            objImg.imde_sizeL = sizeL;
 
            Image_DetailsBLL.Instance.AddImage_Details(objImg);//ajoute l'objet en base
            
            Alert currentAlert = AlertBLL.Instance.GetAlert(pSizeCalculationEventArgs.alertId);
            //on recupere imde_id du dernier enregistrement pour mettre à jour l'alerte
            listID.Clear();
            listID=Image_DetailsBLL.Instance.GetImage_Detailss();
            imdeID = listID[0].imde_id;
            foreach (Image_Details tmpID in listID)
            {
                if (imdeID < tmpID.imde_id)
                    imdeID = tmpID.imde_id;
            }
            currentAlert.imde_id = imdeID;
            //Update de l'alerte !!!! by bubu
            AlertBLL.Instance.UpdateAlert(currentAlert);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        /// <param name="objectDetected"></param>
        /// <returns>double size</returns>
        public double getSizeH(Image<Bgr, byte> image, Rectangle objectDetected)
        {
            Rectangle tmpRec;
            Rectangle tmpRec1;
            this.myImage = image;
            this.myRect = objectDetected;
            double tailleObj;
            Point[] tmpPoints = new Point[20];
            myImage.Draw(objectDetected, new Bgr(Color.Red), 1);
            tmpRec = new Rectangle(10, objectDetected.Bottom, 10, 10);
            tmpRec1 = new Rectangle(10, objectDetected.Top, 10, 10);
            myImage.Draw(tmpRec, new Bgr(Color.Green), 1);
            myImage.Draw(tmpRec1, new Bgr(Color.Green), 1);
            tmpPoints[0] = new Point(0, objectDetected.Bottom);
            tmpPoints[1] = new Point(0, objectDetected.Top);
            //sizeFinal = Math.Round((MathExtended.Distance(tmpPoints[0], tmpPoints[1]) / 2.8), 2) + ((Math.Round((MathExtended.Distance(tmpPoints[0], tmpPoints[1]) / 2.8), 2) * 60) / 100);
            tailleObj = Math.Round((MathExtended.Distance(tmpPoints[0], tmpPoints[1]) / 2.8), 2);
            return tailleObj;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        /// <param name="objectDetected"></param>
        /// <returns>double size</returns>
        public double getSizeL(Image<Bgr, byte> image, Rectangle objectDetected)
        {
            Rectangle tmpRec;
            Rectangle tmpRec1;
            double tailleObj;
            this.myImage = image;
            this.myRect = objectDetected;
            Point[] tmpPoints = new Point[20];
            myImage.Draw(myRect, new Bgr(Color.Red), 1);
            tmpRec = new Rectangle(10, myRect.Left, 10, 10);
            tmpRec1 = new Rectangle(10, myRect.Right, 10, 10);
            myImage.Draw(tmpRec, new Bgr(Color.Green), 1);
            myImage.Draw(tmpRec1, new Bgr(Color.Green), 1);
            tmpPoints[0] = new Point(myRect.Left, 0);
            tmpPoints[1] = new Point(myRect.Right, 0);
            //sizeFinal = Math.Round((MathExtended.Distance(tmpPoints[0], tmpPoints[1]) / 2.8), 2) + ((Math.Round((MathExtended.Distance(tmpPoints[0], tmpPoints[1]) / 2.8), 2) * 60) / 100);
            tailleObj = Math.Round((MathExtended.Distance(tmpPoints[0], tmpPoints[1]) / 2.8), 2);
            return tailleObj;
        }

    }
}
